package com.orderplus.opshop.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class IdcardInfoExtractorUtil {
	/**
	 * 获取出生年份
	 *
	 * @return
	 */
	public static int getYear(String idcard) throws Exception {
		return getCalendar(idcard).get(Calendar.YEAR);
	}

	/**
	 * 获取出生月份
	 *
	 * @return
	 */
	public static int getMonth(String idcard) throws Exception {
		return getCalendar(idcard).get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取出生日期
	 *
	 * @return
	 */
	public static int getDay(String idcard) throws Exception {
		return getCalendar(idcard).get(Calendar.DAY_OF_MONTH);
	}

	private static GregorianCalendar getCalendar(String idcard) throws Exception {
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		String birthday = idcard.substring(6, 14);
		Date birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
		gregorianCalendar.setTime(birthdate);
		return gregorianCalendar;
	}

	/**
	 * 获取性别
	 *
	 * @return
	 */
	public static String getGender(String idcard) throws Exception {
		String gender;
		String id17 = idcard.substring(16, 17);
		if (Integer.parseInt(id17) % 2 != 0) {
			gender = "男";
		} else {
			gender = "女";
		}
		return gender;
	}


	public static void main(String[] args) throws Exception {
		String idcard = "441781198301295427";
		System.out.println(IdcardInfoExtractorUtil.getMonth(idcard));
	}
}
